<% if false # the license inside this if block assertains to this file -%>
# Copyright 2017 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
<% end -%>
<%# Requires: resource - name of resource being fetched -%>
<%#           imports - name of property being fetched -%>
<%= compile('templates/license.erb') -%>

<%= lines(autogen_notice :ruby) -%>

<%
  requires = []
  requires << 'puppet/property'
  requires << "google/#{prop_ns_dir}/property/array" if emit_array
  requires << 'google/object_store'
-%>
<%= lines(emit_requires(requires)) -%>

module Google
  module <%= product_ns %>
    module Data
      # Base class for ResourceRefs
      # Imports <%= imports -%> from <%= resource %>
<%=
  lines(indent(emit_rubocop(binding, :class,
                            ['Google', product_ns, 'Data',
                             class_name].join('::'),
                            :disabled), 6))
-%>
      class <%= class_name %>
        include Comparable

        def ==(other)
          return false unless other.is_a? <%= class_name %>
          return false if resource != other.resource
          true
        end

        def <=>(other)
          resource <=> other.resource
        end
      end

      # A class to fetch the resource value from a referenced block
      # Will return the value exported from a different Puppet resource
<%=
  lines(indent(emit_rubocop(binding, :class,
                            ['Google', product_ns, 'Data',
                             "#{class_name}Title"].join('::'),
                            :disabled), 6))
-%>
      class <%= class_name %>Catalog < <%= class_name %>
        def initialize(title)
          @title = title
        end

        # Puppet requires the title for autorequiring
        def autorequires
          [@title]
        end

        def to_s
          resource.to_s
        end

        def to_json(_arg = nil)
          return if resource.nil?
          resource.to_json
        end

        def resource
<% out_name = property.resource_ref.out_name -%>
<%=
  lines(format(
    [
      ["Google::ObjectStore.instance[:#{out_name}].each do |entry|"],
      [
        "Google::ObjectStore.instance[:#{out_name}]",
        '                   .each do |entry|'
      ],
    ], 10))
-%>
            return entry.exports[:<%= imports -%>] if entry.title == @title
          end
          raise ArgumentError, "<%= out_name -%>[#{@title}] required"
        end
      end

      # A class to manage a JSON blob from GCP API
      # Will immediately return value from JSON blob without changes
<%=
  lines(indent(emit_rubocop(binding, :class,
                            ['Google', product_ns, 'Data',
                             "#{class_name}Resource"].join('::'),
                            :disabled), 6))
-%>
      class <%= class_name %>Api < <%= class_name %>
        attr_reader :resource

        def initialize(resource)
          @resource = resource
        end

        def to_s
          @resource.to_s
        end

        def to_json(_arg = nil)
          @resource.to_json
        end
      end
    end

    module Property
      # A class to manage fetching <%= imports -%> from a <%= resource %>
      class <%= class_name %> < Puppet::Property
        # Used for catalog values
        def unsafe_munge(value)
          self.class.unsafe_munge(value)
        end

        def self.unsafe_munge(value)
          return if value.nil?
          Data::<%= class_name %>Catalog.new(value)
        end

        # Used for fetched JSON values
        def self.api_munge(value)
          return if value.nil?
          Data::<%= class_name %>Api.new(value)
        end
      end
<% if emit_array -%>

      # A Puppet property that holds an integer
      class <%= class_name %>Array < Google::<%= product_ns -%>::Property::Array
        # Used for parsing Puppet catalog
        def unsafe_munge(value)
          self.class.unsafe_munge(value)
        end

        # Used for parsing Puppet catalog
        def self.unsafe_munge(value)
          return if value.nil?
          return <%= class_name %>.unsafe_munge(value) \
            unless value.is_a?(::Array)
          value.map { |v| <%= class_name %>.unsafe_munge(v) }
        end

        # Used for parsing GCP API responses
        def self.api_munge(value)
          return if value.nil?
          return <%= class_name %>.api_munge(value) \
            unless value.is_a?(::Array)
          value.map { |v| <%= class_name %>.api_munge(v) }
        end
      end
<% end # if emit_array -%>
    end
  end
end
